﻿/*
=======================================================
    Wirerope【钢丝绳】
=======================================================
*/

namespace Services
{
    public interface IWireropeLogic : ILogic<WireropeModel, WireropeFilterModel>
    {
        int UpdateIsDelete(bool flag, params int[] ids);
    }


    public class WireropeLogic : BaseLogic<WireropeModel, WireropeFilterModel, Wirerope, IWireropeRepository, IWireropeMapping, IWireropeService>, IWireropeLogic
    {
        private readonly IDataDepartmentRepository _IDataDepartmentRepository;
        private readonly IDepartmentRepository _IDepartmentRepository;
        private readonly ICreateCode _ICreateCode;

        public WireropeLogic(ICurrentUser currentUser, IWireropeRepository repository, IWireropeMapping mapping, IWireropeService service, IDataDepartmentRepository iDataDepartmentRepository, IDepartmentRepository iDepartmentRepository, ICreateCode iCreateCode)
            : base(currentUser, repository, mapping, service)
        {
            _IDataDepartmentRepository = iDataDepartmentRepository;
            _IDepartmentRepository = iDepartmentRepository;
            _ICreateCode = iCreateCode;
        }

        #region 启用/禁用  UpdateIsDelete
        public int UpdateIsDelete(bool flag, params int[] ids)
        {
            return _repository.UpdateIsDelete(flag, ids);
        }
        #endregion

        #region GetPage
        public override PageModel<WireropeModel> GetPage(WireropeFilterModel filterModel, int pageIndex = 1, int pageSize = 20, string orderBy = null)
        {
            var pageData = base.GetPage(filterModel, pageIndex, pageSize, orderBy);

            //获取当前用户应该有的数据权限范围
            var (isAll, dataPermissionDepartmentIDs) = _currentUser.CurrentUserInfo.LoginUserRoleDepartment;

            //获取所有部门设置的数据权限
            List<DataDepartment> departmentDataPermissions = _IDataDepartmentRepository.GetDataDepartmentsByDataIDs(DataDepartment_Type.钢丝绳管理, pageData.Items.Select(x => x.WireropeID).ToArray());

            var departmentList = _IDepartmentRepository.GetAll();

            foreach (var item in pageData.Items)
            {
                //获取当前循环这条部门数据的所设置的数据权限
                var _currentDataPermissions = departmentDataPermissions.Where(x => x.DataID == item.WireropeID).Select(x => x.DepartmentID).ToList();

                //设置每一条数据的数据权限的部门名称
                if (_currentDataPermissions.Any(x => x == Gx._NO_DATA_PERMISSION_VALUE))
                {
                    item.SelectDepartmentNames = new List<string>() { "不限" };
                }
                else
                {
                    item.SelectDepartmentNames = departmentList.Where(x => _currentDataPermissions.Contains(x.DepartmentID)).Select(x => x.Name).ToList();
                }
            }
            return pageData;
        }
        #endregion

        #region Insert
        public override WireropeModel Insert(WireropeModel model)
        {
            if (model.Code.IsEmpty())
            {
                model.Code = _ICreateCode.GetWireropeCode();
            }

            if (_ICreateCode.WireropeCodeExists(model.Code))
            {
                throw new CustomException("编码重复！");
            }

            var res = base.Insert(model);
            _IDataDepartmentRepository.InsertByDataID(DataDepartment_Type.钢丝绳管理, res.WireropeID, model.SelectDepartmentIDs.ToArray());
            return res;
        }
        #endregion

        #region Update
        public override int Update(WireropeModel model)
        {
            _IDataDepartmentRepository.UpdateByDataID(DataDepartment_Type.钢丝绳管理, model.WireropeID, model.SelectDepartmentIDs.ToArray());
            return base.Update(model);
        }
        #endregion

        #region GetByID
        public override WireropeModel GetByID(int id)
        {
            var item = base.GetByID(id);
            item.SelectDepartmentIDs = _IDataDepartmentRepository.GetDataDepartmentsByDataIDs(DataDepartment_Type.钢丝绳管理, id).Select(x => x.DepartmentID).ToList();
            return item;
        }
        #endregion
    }
}
